var app = getApp()
Page({
  data: {

  },
  onLoad: function (e) {
    var that = this;
    console.log(e.id);
    that.setData({
      cardid: e.id
    })
    that.requestCardDetail();
    that.requestStorelist();
  },
  onShow: function () {
    var that = this;
    app.setNavtitle('会员卡详情');
  },
  // 请求会员卡详情
  requestCardDetail: function () {
    var that = this;
    var cardid = that.data.cardid;
    app.setVersion(that);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_member_card_details',
        cardid: cardid
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var cardRights = (res.data.data.rights).split('<br/>');
          var cardNotice = (res.data.data.notice).split('<br/>');
          that.setData({
            cardDetail: res.data.data,
            cardRights: cardRights,
            cardNotice: cardNotice
          })
          console.log(that.data.cardDetail);
        } else {
          that.setData({
            cardDetail: ''
          })
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  // 请求门店列表
  requestStorelist: function () {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_store_list'
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data);
          that.setData({
            storeList: res.data.data,
            curStoreid: res.data.data ? res.data.data[0].id : ''
          })
          console.log(that.data.storeList);
        } else {
          that.setData({
            storeList: []
          })
        }
      },
      complete: function () {
      }
    });
  },
  toggleStore: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      curStoreid: id
    })
  },
  // tocardOrder: function () {
  //   var that = this;
  //   var storeid = that.data.curStoreid;
  //   var cardid = that.data.cardDetail.id;
  //   var data = {
  //     map: 'applet_buy_member_card',
  //     cardid: cardid,
  //     stid: storeid
  //   }
  //   that.requestOpenCard(data);
  // },
  tocardOrder: function () {
    var that = this;
    var storeid = that.data.curStoreid;
    var cardid = that.data.cardDetail.id;
    var price = that.data.cardDetail.price;
    wx.navigateTo({
      url: '/subpages/ciCardPay/ciCardPay?cardid=' + cardid + '&storeid=' + storeid+'&price='+price,
    })
  },
  requestOpenCard: function (data) {
    var that = this;
    console.log(data);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data);
          wx.setStorage({
            key: 'cardOrderInfo',
            data: res.data.data,
            success: function () {
              wx.navigateTo({
                url: '/subpages/cardOrderPay/cardOrderPay',
              })
            }
          })
        } else {
          console.log(res.data);
          app.errorTip(that, res.data.msg, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
})